<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Format\AdminRoleFormat;
use App\Format\AdminUserFormat;
use App\Http\Requests\CreateAdminRoleRequest;
use App\Services\AdminAuthService;
use Illuminate\Http\Request;

class AdminAuthController extends Controller
{
    protected $adminAuthService;

    public function __construct(AdminAuthService $adminAuthService)
    {
        $this->adminAuthService = $adminAuthService;
    }

    public function getRoles()
    {
        $res = $this->adminAuthService->getRoles();
        return renderSuccessJson($res);
    }

    public function getActions()
    {
        $res = $this->adminAuthService->getActions();
        return renderSuccessJson($res);
    }

    public function setRolesActions(Request $request)
    {
        $roleId = $request->input('admin_role_id');
        if (empty($roleId)) {
            throw new ApiException('参数不合法', 1);
        }
        $actionIds = $request->input('admin_action_uuids');
        $this->adminAuthService->setActions($roleId, $actionIds);
        return renderSuccessJson();
    }

    public function getUserRoleActions(Request $request)
    {
        $userId = $request->input('user_id');
        $res = $this->adminAuthService->getUserRoleActions($userId);
        return renderSuccessJson($res);
    }

    public function getAdminUsers(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];

        if ($request->input('name')) {
            $search['name'] = $request->input('name');
        }

        $res = $this->adminAuthService->getAdminUsers($page, $pageSize, $search);
        return renderSuccessJson($res);
    }

    public function disabledAdminUser(Request $request)
    {
        $adminUserId = $request->input('admin_user_id');
        $this->adminAuthService->disabledAdminUser($adminUserId);
        return renderSuccessJson();
    }

    public function enableAdminUser(Request $request)
    {
        $adminUserId = $request->input('admin_user_id');
        $this->adminAuthService->enableAdminUser($adminUserId);
        return renderSuccessJson();
    }

    public function createRole(CreateAdminRoleRequest $createAdminRoleRequest)
    {
        $format = new AdminRoleFormat($createAdminRoleRequest->all());
        $this->adminAuthService->createAdminRole($format);
        return renderSuccessJson();
    }

    public function updateRole(Request $request)
    {
        $format = new AdminRoleFormat($request->all());
        $this->adminAuthService->updateAdminRole($format);
        return renderSuccessJson();
    }

    public function getRoleActions(Request $request)
    {
        $roleId = $request->input('role_id');
        $res = $this->adminAuthService->getRoleActions($roleId);
        return renderSuccessJson($res);
    }

    public function setUserRole(Request $request)
    {
        $roleId = $request->input('role_id');
        $handleUserId = $request->input('handle_user_id');
        $this->adminAuthService->setUserRole($handleUserId, $roleId);
        return renderSuccessJson();
    }

    public function updateAdminUser(Request $request)
    {
        $format = new AdminUserFormat($request->all());
        $this->adminAuthService->updateAdminUser($format);
        return renderSuccessJson();
    }
}
